package trustedPlatformBackup;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.Properties;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.DirectoryDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TabItem;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

import trustedPlatformBackup.VCMainWindow.SortFile;

public class CGUIHandler {

	private VCMainWindow guiMainWindow;
	private Display display;
	private Shell sShell = null;
	private ArrayList<DayBackup> dayBackups = new ArrayList<DayBackup>();
	private ProgramProperties properties = null;
	private int daySlectionIndex = 0;
	private int daySlectionIndexOld = 0;
	private String[] strRelToData = null;
	private String strIncbackup = "";
	private String strIncbackupOld;

	void Properties() {

	}

	public void guiRun() {

		try {

			display = Display.getDefault();
			guiMainWindow = new VCMainWindow();
			properties = new ProgramProperties();
			properties.load(null);
			guiMainWindow.createSShell();

			guiMainWindowInit();

			guiMainWindow.getsShell().open();

			guiMainWindow.getsShell().layout();
			while (!guiMainWindow.getsShell().isDisposed()) {
				if (!display.readAndDispatch())
					display.sleep();
			}
			display.dispose();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	private void guiMainWindowInit() {
		createListener();
		hr(properties.getCurrentBackupFolder());

		fillFolderTree((new File(properties.getDataFolder())), null, true, true);
		fillDateList();
		loadPropertiesToGUI();
		setURLFile();
	}

	private void createListener() {
		// Add Listener
		// TAB Options ------------------
		guiMainWindow.getwSavePropertiesButton().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				savePropertiesFromGUI();
				properties.save(null);

				fillDateList();
			}
		});

		guiMainWindow.getwSetDefaultButton().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				loadDefaultPropertiesToGUI();
			}
		});

		// Suche... buttons

		guiMainWindow.getwFindECryptFSMountPointFolderButton().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				setOptionDirs(guiMainWindow.getwFindECryptFSMountPointFolderText());
			}
		});
		guiMainWindow.getwFindECryptFSSrcFolderButton().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				setOptionDirs(guiMainWindow.getwFindECryptFSSrcFolderText());
			}
		});
		guiMainWindow.getwFindScriptFolderButton().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				setOptionDirs(guiMainWindow.getwTPMInitScriptText());
			}
		});
		guiMainWindow.getwFindDataFolderButton().addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				setOptionDirs(guiMainWindow.getwDataFolderText());
			}
		});
		guiMainWindow.getwFindBackupFolderButton().addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent event) {
				setOptionDirs(guiMainWindow.getwBackupFolderText());

			}
		});

		// TAB Backup ------------------
		// FileTable
		guiMainWindow.getwFileTable().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				TreeItem item = ((TreeItem) guiMainWindow.getwURLText().getData());

				if (item != null && ((File) item.getData()).isDirectory()) {
					if (!item.getExpanded()) {
						item.setExpanded(true);
						expandTree(item, false);

					}
					guiMainWindow.getwFolderTree().select(item);
					// TODO select von gestern
				}
				if (guiMainWindow.getwFileTable().getSelectionCount() > 0) {
					guiMainWindow.getwURLFileText().setText(((File) guiMainWindow.getwFileTable().getSelection()[0].getData()).getAbsolutePath());
					guiMainWindow.getwURLFileText().setData(((File) guiMainWindow.getwFileTable().getSelection()[0].getData()));
				}

			}
		});
		guiMainWindow.getwFileTable().addMouseListener(new org.eclipse.swt.events.MouseAdapter() {
			public void mouseDoubleClick(org.eclipse.swt.events.MouseEvent e) {
				TreeItem item = ((TreeItem) guiMainWindow.getwURLText().getData());

				if (item != null && ((File) item.getData()).isDirectory()) {
					if (!item.getExpanded()) {
						item.setExpanded(true);

						expandTree(item, true);
						item.setExpanded(true);
					} else {
						for (TreeItem tItem : item.getItems()) {

							if (guiMainWindow.getwFileTable().getSelectionCount() > 0
									&& ((File) tItem.getData()).getAbsolutePath().equals(((File) guiMainWindow.getwFileTable().getSelection()[0].getData()).getAbsolutePath())) {
								guiMainWindow.getwFolderTree().select(tItem);
								expandTree(tItem, false);
								tItem.setExpanded(true);
								hr(((File) tItem.getData()).getAbsolutePath());
								setPath(tItem);
							}
						}
					}
				}
			}
		}

		);

		// FolderTree
		guiMainWindow.getwFolderTree().addListener(SWT.Expand, new Listener() {
			public void handleEvent(Event e) {
				expandTree((TreeItem) e.item, false);

			}
		});
		guiMainWindow.getwFolderTree().addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				TreeItem item = (TreeItem) e.item;
				setPath(item);

			}
		});

		// DayBackupList
		guiMainWindow.getwDayBackupList().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				if (guiMainWindow.getwDayBackupList().getSelectionCount() > 0) {
					setDaySlectionIndex(guiMainWindow.getwDayBackupList().getSelectionIndex());
					setURLFile();
					switch (guiMainWindow.getwDayBackupList().getSelectionIndex()) {
					case 0:
						fillFolderTree((new File(properties.getDataFolder())), null, true, true);
						fillTimeList(guiMainWindow.getwDayBackupList().getSelection()[0]);
						break;

					case 1:
						fillFolderTree((new File(properties.getCurrentBackupFolder())), null, true, false);
						fillTimeList(guiMainWindow.getwDayBackupList().getSelection()[0]);
						break;

					default: {
						fillTimeList(guiMainWindow.getwDayBackupList().getSelection()[0]);
						if (guiMainWindow.getwTimeBackupList().getItemCount() > 0) {
							guiMainWindow.getwTimeBackupList().select(0);
							fillFolderTreeOnClick();
						}
					}
						break;
					}
					selectTreeItem(strRelToData, guiMainWindow.getwFolderTree().getItems(), 0);
				}
			}
		});
		// Versionsliste click
		guiMainWindow.getwFileVersionsTable().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				if (guiMainWindow.getwFileVersionsTable().getItemCount() > 0 && guiMainWindow.getwFileVersionsTable().getSelectionCount() > 0) {
					File file = (File) guiMainWindow.getwFileVersionsTable().getSelection()[0].getData();
					guiMainWindow.getwURLFileText().setText(file.getAbsolutePath());
					guiMainWindow.getwURLFileText().setData(file);

				}

			}
		});
		// TimeBackupList click
		guiMainWindow.getwTimeBackupList().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				fillFolderTreeOnClick();
				selectTreeItem(strRelToData, guiMainWindow.getwFolderTree().getItems(), 0);
			}
		});

		// alle versionen anzeigen button
		guiMainWindow.getwViewAllFileVersionsButton().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				String filename = (new File(guiMainWindow.getwURLText().getText())).getName();
				String substring = "";

				switch (guiMainWindow.getwDayBackupList().getSelectionIndex()) {
				case 0:
					substring = guiMainWindow.getwURLFileText().getText().substring(properties.getDataFolderFile().getParent().length());
					break;
				case 1:
					substring = guiMainWindow.getwURLFileText().getText().substring(properties.getCurrentBackupFolder().length());
					hr();
					hr(substring);
					hr();
					break;
				default:
					substring = guiMainWindow.getwURLFileText().getText().substring(strIncbackup.length());

					break;
				}
				guiMainWindow.getwFileVersionsTable().clearAll();
				guiMainWindow.getwFileVersionsTable().setItemCount(0);
				if (guiMainWindow.getwDayBackupList().getSelectionIndex() >= 0) {

					String[] backuplist = properties.getIncBackupFolderFile().list();
					ArrayList<String> fileList = new ArrayList<String>();
					String strO = properties.getCurrentBackupFolder() + substring;

					File checkfile = new File(strO);
					if (checkfile.isDirectory() || checkfile.isFile()) {
						hr("testtest" + strO);

						TableItem tableItem = new TableItem(guiMainWindow.getwFileVersionsTable(), SWT.NONE);
						SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
						tableItem.setText(new String[] { "Aktuelles Backup", checkfile.getName(), Long.toString(checkfile.length() / 1000) + " KB", sdf.format(new Date(checkfile.lastModified())),
								checkfile.getAbsolutePath() });
						tableItem.setData(checkfile);
					}

					for (String strfile : backuplist) {
						strO = properties.getIncBackupFolder() + File.separator + strfile + substring;
						checkfile = new File(strO);
						if (checkfile.isDirectory() || checkfile.isFile()) {
							hr(strO);
							fileList.add(strO);
							TableItem tableItem = new TableItem(guiMainWindow.getwFileVersionsTable(), SWT.NONE);

							SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
							tableItem.setText(new String[] { strfile, checkfile.getName(), Long.toString(checkfile.length() / 1000) + " KB", sdf.format(new Date(checkfile.lastModified())),
									checkfile.getAbsolutePath() });
							tableItem.setData(checkfile);
						}
					}

				}

			}
		});

		// wiederherstellen recover
		guiMainWindow.getwRecoverFileButton().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				MessageBox messageBox = null;
				File file = (File) guiMainWindow.getwURLFileText().getData();
				if (guiMainWindow.getwDayBackupList().getSelectionIndex() == 0) {
					messageBox = new MessageBox(new Shell(), SWT.OK);
					messageBox.setMessage("Bitte eine Backupdatei oder ein Backupverzeichnis auswählen!");
					messageBox.setText("Backup!");
					messageBox.open();
					return;
				}

				if (file != null && file.isDirectory()) {
					DirectoryDialog dlg = new DirectoryDialog(new Shell());
					dlg.setFilterPath(properties.getDataFolderFile().getParent() + File.separator + getRelPath());
					dlg.setText("Datenverzeichnis");
					dlg.setMessage("Verzeichnis auswählen");
					String dir = "";
					messageBox = new MessageBox(new Shell(), SWT.ERROR | SWT.OK);
					messageBox.setMessage("Verzeichnis ist nicht leer!");
					messageBox.setText("Verzeichnis!");
					do {
						dir = dlg.open();
						hr(dir);
					} while (dir != null && new File(dir).isDirectory() && new File(dir).listFiles().length > 0 && messageBox.open() == SWT.OK);

					if (dir != null) {

						hr("erstellen  " + dir);
						try {
							copyDirectory(file, new File(dir));
						} catch (IOException e1) {
							messageBox = new MessageBox(new Shell(), SWT.ERROR | SWT.OK);
							messageBox.setMessage("Verzeichnis konnte nicht kopiert werden!");
							messageBox.setText("Fehler beim kopieren!");
							messageBox.open();
						}
					}
				} else if (file != null && file.isFile()) {
					FileDialog dialog = new FileDialog(new Shell(), SWT.SAVE);
					dialog.setFilterNames(new String[] { "Datei Wiederherstellen", "All Files (*.*)" });
					dialog.setFilterExtensions(new String[] { "*.*" }); // Windows
					// wild
					// cards

					dialog.setFilterPath(properties.getDataFolderFile().getParent() + File.separator + getRelPath()); // Windows
					// path
					dialog.setFileName(file.getName());

					messageBox = new MessageBox(new Shell(), SWT.ICON_QUESTION | SWT.YES | SWT.NO);
					messageBox.setMessage("Datei existiert! Soll diese überschrieben werden?");
					messageBox.setText("Datei überschreiben?");
					String strFile = "";
					do {
						strFile = dialog.open();
					} while (strFile != null && new File(strFile).isFile() && messageBox.open() == SWT.NO);

					if (strFile != null) {
						try {
							copyDirectory(file, new File(strFile));
						} catch (IOException e1) {
							messageBox = new MessageBox(new Shell(), SWT.ERROR | SWT.OK);
							messageBox.setMessage("Datei konnte nicht kopiert werden!");
							messageBox.setText("Fehler beim kopieren!");
							messageBox.open();
						}

					}
				}

			}
		});
		// fullBackup
		guiMainWindow.getwDoFullBackupButton().addSelectionListener(new org.eclipse.swt.events.SelectionAdapter() {
			public void widgetSelected(org.eclipse.swt.events.SelectionEvent e) {
				CCMDHandler a = new CCMDHandler();
				if (properties.getHardlinkMode()) {
					a.makeFullBackup(properties.getDoBackupScript(), true);
				} else {
					a.makeFullBackup(properties.getDoBackupScript(), true);
				}
				refreshGUI();
			}
		});
	}

	void setOptionDirs(Text wText) {
		DirectoryDialog dlg = new DirectoryDialog(new Shell());
		dlg.setFilterPath(wText.getText());
		dlg.setText("MountPoint Verzeichnis");
		dlg.setMessage("Verzeichnis auswählen");
		String dir = dlg.open();
		if (dir != null) {
			wText.setText(dir);
		}

	}

	protected String getRelPath() {
		String strDirToData;
		File file;
		try {
			file = ((File) guiMainWindow.getwURLFileText().getData());
		} catch (Exception e) {
			file = null;
		}
		if (file != null) {

			String strRelPath = file.getAbsolutePath();
			switch (guiMainWindow.getwDayBackupList().getSelectionIndex()) {
			case 0:
				strDirToData = (new File(properties.getDataFolder()).getParent());
				break;
			case 1:
				strDirToData = properties.getCurrentBackupFolder();
				break;

			default:
				strDirToData = strIncbackup;
				break;
			}

			if (strRelPath.length() > strDirToData.length() && strRelPath.indexOf(strDirToData) > -1) { // &&
				// strRelPath.length()>strRelPath)
				// {
				return strRelPath.substring(strDirToData.length() + 1, strRelPath.length());
			}
		}
		return "";

	}

	protected void setDaySlectionIndex(int selectionIndex) {
		daySlectionIndexOld = daySlectionIndex;
		daySlectionIndex = selectionIndex;

	}

	protected DayBackup getDayBackup(String strDay) {

		for (DayBackup lDayBackup : dayBackups) {
			if (lDayBackup.date.equals(strDay)) {
				return lDayBackup;
			}
		}
		return null;
	}

	private void fillFolderTree(File root, File currentFile, boolean expand, boolean isData) {

		guiMainWindow.getwFileTable().removeAll();
		guiMainWindow.getwFileTable().setItemCount(0);
		guiMainWindow.getwFolderTree().removeAll();

		if (root != null && (root.isFile() || root.isDirectory())) {
			TreeItem treeItem1 = null;
			if (isData) {
				treeItem1 = new TreeItem(guiMainWindow.getwFolderTree(), SWT.NONE);
				treeItem1.setText(root.getName());
				treeItem1.setData(root);
				treeItem1.setImage(CCommonConstants.IMAGE_FOLDER);

			}
			File[] roots = (File[]) root.listFiles();
			for (int i = 0; i < roots.length; i++) {
				File file = roots[i];
				if (file.isDirectory()) {
					TreeItem treeItem;
					if (isData)
						treeItem = new TreeItem(treeItem1, SWT.NONE);
					else
						treeItem = new TreeItem(guiMainWindow.getwFolderTree(), SWT.NONE);
					treeItem.setText(file.getName());
					treeItem.setData(file);
					treeItem.setImage(CCommonConstants.IMAGE_FOLDER);
					if (file.list() != null && file.list().length != 0) {
						TreeItem a = new TreeItem(treeItem, SWT.NONE);
					}
					if (expand && !isData) {
						hr("expand:"+treeItem.getText());
						expandTree(treeItem, false);
						treeItem.setExpanded(true);

					}

					if (isData) {
						guiMainWindow.getwFolderTree().select(treeItem1);

						setPath(treeItem1);
						treeItem1.setExpanded(true);
					} else {
						guiMainWindow.getwFolderTree().select(treeItem);
						setPath(treeItem);
					}
				}

			}
		}
	}

	void selectTreeItem(String[] path, TreeItem[] tItems, int depth) {
		if (path != null && path.length > depth) {

			hr("ss:" + path[depth]);
			for (TreeItem tItem : tItems) {
				if (tItem.getText().equals(path[depth])) {
					expandTree(tItem, false);

					setPath(tItem);
					guiMainWindow.getwFolderTree().select(tItem);
					tItem.setExpanded(true);
					selectTreeItem(path, tItem.getItems(), depth + 1);
					// if (path!=null && path.length == depth + 1)
					
				}
			}
		}

	}

	private void setURLFile() {
		File file;
		try {
			file = ((File) ((TreeItem) guiMainWindow.getwURLText().getData()).getData());
		} catch (Exception e) {
			file = null;
		}

		if (file != null) {
			hr("not null!!! File:" + file.getAbsolutePath());
			String strRelPath = file.getAbsolutePath();
			String strDirToData = "";
			switch (daySlectionIndexOld) {

			case 0:
				strDirToData = (new File(properties.getDataFolder()).getParent());

				break;

			case 1:
				strDirToData = properties.getCurrentBackupFolder();

				break;

			default:
				strDirToData = strIncbackup;

				break;
			}

			if (strRelPath.length() > strDirToData.length() && strRelPath.indexOf(strDirToData) > -1) { // &&
				// strRelPath.length()>strRelPath)
				// {
				hr("strRelPath;   " + strRelPath);
				hr("strDirToData;  " + strDirToData);
				String strRelPath1 = strRelPath.substring(strDirToData.length() + 1, strRelPath.length());
				hr("strRelPath1;   " + strRelPath1);
				strRelToData = strRelPath1.split(File.separator.equals("\\") ? "\\\\" : "/");
				hr(strRelToData[0]);

			}
		} else {
			hr("null!!!");
		}

	}

	protected void fillFolderTreeOnClick() {
		if (guiMainWindow.getwDayBackupList().getSelectionCount() > 0 && guiMainWindow.getwTimeBackupList().getSelectionCount() > 0) {
			DayBackup dayBackup = getDayBackup(guiMainWindow.getwDayBackupList().getSelection()[0]);
			int index = guiMainWindow.getwTimeBackupList().getSelectionIndex();
			strIncbackupOld = strIncbackup;
			strIncbackup = properties.getIncBackupFolder() + File.separator + dayBackup.date + "_" + dayBackup.times.get(index) + "_" + dayBackup.type.get(index);
			File file = new File(strIncbackup);
			// File f = (File)guiMainWindow.getwURLText().getData(); TODO:
			// current file
			setURLFile();
			fillFolderTree(file, null, true, false);
		}
	}

	private void expandTree(TreeItem treeItem, boolean tSelect) {

		if (treeItem == null)
			return;
		if (treeItem.getItemCount() == 1) {
			TreeItem firstItem = treeItem.getItems()[0];
			if (firstItem.getData() != null)
				return;
			firstItem.dispose();
		} else {
			return;
		}
		File root = (File) treeItem.getData();
		File[] files = (File[]) root.listFiles();
		if (files == null)
			return;

		for (int i = 0; i < files.length; i++) {
			File file = files[i];

			if (file.isDirectory()) {

				TreeItem treeItem1 = new TreeItem(treeItem, SWT.NONE);
				treeItem1.setText(file.getName());
				treeItem1.setData(file);
				treeItem1.setImage(CCommonConstants.IMAGE_FOLDER);
				if (tSelect && guiMainWindow != null) {
					if (guiMainWindow.getwFileTable().getSelectionCount() > 0 && file.equals(((File) guiMainWindow.getwFileTable().getSelection()[0].getData()).getAbsolutePath())) {
						guiMainWindow.getwFolderTree().select(treeItem1);
					}
					expandTree(treeItem1, false);
					guiMainWindow.getwFolderTree().select(treeItem1);
				}
				if (file.list() != null) {
					TreeItem a = new TreeItem(treeItem1, SWT.NONE);
				}

			}

		}

	}

	public void setPath(TreeItem item) {

		if (item == null)
			return;
		final File root = (File) item.getData();

		guiMainWindow.getwURLText().setData(item);
		guiMainWindow.getwURLText().setText(root.getAbsolutePath() + "");
		guiMainWindow.getwURLFileText().setText(root.getAbsolutePath() + "");
		guiMainWindow.getwURLFileText().setData(root);

		CCMDHandler CDM = new CCMDHandler();
		ArrayList<File> a = CDM.fillFileList(root.getAbsolutePath());
		guiMainWindow.setAlFileTable(new ArrayList<SortFile>());

		for (Iterator iterator = a.iterator(); iterator.hasNext();) {
			guiMainWindow.getAlFileTable().add(guiMainWindow.getNewSortFile(((File) iterator.next()).getAbsolutePath())); // new
			// SortFile(((File)
			// iterator.next()).getAbsolutePath()));
		}
		guiMainWindow.getwFileTable().clearAll();
		guiMainWindow.getwFileTable().setItemCount(guiMainWindow.getAlFileTable().size());
	}

	private void fillDateList() {
		guiMainWindow.getwDayBackupList().removeAll();
		guiMainWindow.getwDayBackupList().add(CCommonConstants.DATA_LABEL);
		guiMainWindow.getwDayBackupList().add(CCommonConstants.CURRENT_BACKUP_LABEL);
		guiMainWindow.getwDayBackupList().select(0);
		File file = new File(properties.getIncBackupFolder());
		File[] filelist = file.listFiles();
		if (filelist != null) {
			hr("null!=filelist");
			for (File file2 : filelist) {
				if (file2.isDirectory()) {
					boolean isExist = false;
					String[] strSplitFilename = file2.getName().split("_");
					if (strSplitFilename.length == 3) {

						for (DayBackup lDayBackup : dayBackups) {

							if (lDayBackup.date.equals(strSplitFilename[0])) {
								isExist = true;

								lDayBackup.times.add(strSplitFilename[1]);
								lDayBackup.type.add(strSplitFilename[2]);

								break;

							}
						}
						if (!isExist) {
							// [0]=Date [1]=Time [2]=type
							DayBackup lDayBackup = new DayBackup(strSplitFilename[0], strSplitFilename[1], strSplitFilename[2]);
							dayBackups.add(lDayBackup);
							guiMainWindow.getwDayBackupList().add(lDayBackup.date);
							isExist = false;
						}

					}
				}
			}

		}
	}

	private void fillTimeList(String strDate) {
		guiMainWindow.getwTimeBackupList().removeAll();
		for (DayBackup lDayBackup : dayBackups) {
			if (lDayBackup.date.equals(strDate)) {
				for (int i = 0; i < lDayBackup.times.size() && i < lDayBackup.type.size(); i++) {
					String sTime = lDayBackup.times.get(i) + "  --" + lDayBackup.type.get(i);
					guiMainWindow.getwTimeBackupList().add(sTime);
				}

			}
		}
	}

	public void loadDefaultPropertiesToGUI() {
		guiMainWindow.getwBackupFolderText().setText(ProgramProperties.BACKUP_FOLDER_DEFAULT_PROPERTY);
		guiMainWindow.getwDataFolderText().setText(ProgramProperties.DATA_FOLDER_DEFAULT_PROPERTY);
		guiMainWindow.getwFindECryptFSMountPointFolderText().setText(ProgramProperties.ECRYPTFS_MOUNTPOINT_FOLDER_DEFAULT_PROPERTY);
		guiMainWindow.getwFindECryptFSSrcFolderText().setText(ProgramProperties.ECRYPTFS_SRC_FOLDER_DEFAULT_PROPERTY);
		guiMainWindow.getwTPMInitScriptText().setText(ProgramProperties.TPMSCRIPTS_FOLDER_DEFAULT_PROPERTY);
		guiMainWindow.getwHardlinkModeCheckBox().setSelection(ProgramProperties.getHARDLINK_MODE_DEFAULT_PROPERTY());

	}

	public void loadPropertiesToGUI() {
		guiMainWindow.getwBackupFolderText().setText(properties.getBackupFolder() + "");
		guiMainWindow.getwDataFolderText().setText(properties.getDataFolder() + "");
		guiMainWindow.getwFindECryptFSMountPointFolderText().setText(properties.getEcryptFSMountPointFolder() + "");
		guiMainWindow.getwFindECryptFSSrcFolderText().setText(properties.getEcryptFSSRCFolder() + "");
		guiMainWindow.getwTPMInitScriptText().setText(properties.getTPMScriptsFolder() + "");
		guiMainWindow.getwHardlinkModeCheckBox().setSelection(properties.getHardlinkMode());

	}

	public void savePropertiesFromGUI() {
		try {

			if (!guiMainWindow.getwDataFolderText().getText().equals("")) {

				File file = new File(guiMainWindow.getwDataFolderText().getText());
				if (file == null || !file.isDirectory()) {
					file.mkdir();
				}

				properties.setDataFolder(guiMainWindow.getwDataFolderText().getText());
			}
			if (!guiMainWindow.getwBackupFolderText().getText().equals("")) {
				File file = new File(guiMainWindow.getwDataFolderText().getText());
				if (file == null || !file.isDirectory()) {
					file.mkdir();
				}
				properties.setBackupFolder(guiMainWindow.getwBackupFolderText().getText());
				file = new File(properties.getIncBackupFolder());
				file.mkdir();
				file = new File(properties.getLogBackupFolder());
				file.mkdir();
			}

			if (!guiMainWindow.getwFindECryptFSMountPointFolderText().getText().equals("")) {
				properties.setEcryptFSMountPointFolder(guiMainWindow.getwFindECryptFSMountPointFolderText().getText());
			}
			if (!guiMainWindow.getwFindECryptFSSrcFolderText().getText().equals("")) {
				properties.setEcryptFSSRCFolder(guiMainWindow.getwFindECryptFSSrcFolderText().getText());
			}

			if (!guiMainWindow.getwTPMInitScriptText().getText().equals("")) {
				properties.setTPMScriptsFolderFolder(guiMainWindow.getwTPMInitScriptText().getText());
			}

			properties.setHardlinkMode(guiMainWindow.getwHardlinkModeCheckBox().getSelection());

		} catch (Exception e) {
			MessageBox messageBox = new MessageBox(new Shell(), SWT.ERROR | SWT.OK);
			messageBox.setMessage("Verzeichnisse konnte nicht erstellt bzw. gefunden werden!");
			messageBox.setText("Fehler beim speichen!");
			messageBox.open();
		}

	}

	// Helper Methoden
	private void hr() {
		System.out.println("----------------------------");

	}

	private void hr(Object o) {
		System.out.println("----------------------------");
		System.out.println(o);
		System.out.println("----------------------------");

	}

	// Kopiert Verzeichnisse und Dateien recursiv
	public void copyDirectory(File sourceLocation, File targetLocation) throws IOException {

		if (sourceLocation.isDirectory()) {
			if (!targetLocation.exists()) {
				targetLocation.mkdir();
			}

			String[] children = sourceLocation.list();
			for (int i = 0; i < children.length; i++) {
				copyDirectory(new File(sourceLocation, children[i]), new File(targetLocation, children[i]));
			}
		} else {

			InputStream in = new FileInputStream(sourceLocation);
			OutputStream out = new FileOutputStream(targetLocation);

			// Copy the bits from instream to outstream
			byte[] buf = new byte[1024];
			int len;
			while ((len = in.read(buf)) > 0) {
				out.write(buf, 0, len);
			}
			in.close();
			out.close();
		}
	}

	public void refreshGUI() {
		dayBackups = new ArrayList<DayBackup>();
		fillFolderTree((new File(properties.getDataFolder())), null, true, true);
		fillDateList();
		setURLFile();
	}
}
